package com.macro.mall.common.api.util;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * class
 *
 * @author xuyanjun
 * @date 2019-12-09
 */
public class StringUtils extends StrUtil {

    /**
     * 0元金额
     */
    public static final String ZERO_FOR_MONEY = "0.00";

    /**
     * 数字
     */
    private static Character[] NUMBERS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};

    /**
     * 小写字母
     */
    private static Character[] LOWER_LETTERS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    /**
     * 大写字母
     */
    private static Character[] UPPER_LETTERS = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    /**
     * EMOJI 表情pattern
     */
    private static Pattern EMOJI_PATTERN = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
            Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

    /**
     * 字符串是否是合法的电话(长度及是否纯数字)
     */
    public static boolean isMobileNo(String str) {
        if (isBlank(str)) {
            return false;
        }

        String regExp = "((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0-9])|(18[0-9])|166|191|192|198|199|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 手机号码消敏
     *
     * @param mobileNo 手机号码
     * @return 手机号码
     */
    public static String desensiteMobileNo(String mobileNo) {
        if (isBlank(mobileNo)) {
            return mobileNo;
        }
        if (mobileNo.length() != 11) {
            return mobileNo;
        }
        return mobileNo.substring(0, 3) + "****" + mobileNo.substring(7, 11);
    }

    /**
     * 获取随机数字
     *
     * @param length 长度
     * @return 随机数字
     */
    public static String getRandomNumbers(int length) {
        return getRandomString(NUMBERS, length);
    }

    /**
     * 获取随机数字和字母
     *
     * @param length 长度
     * @return 随机数字和字母
     */
    public static String getRandomNumberAndLetters(int length) {
        Character[] characters = ArrayUtil.addAll(NUMBERS, LOWER_LETTERS, UPPER_LETTERS);

        return getRandomString(characters, length);
    }

    /**
     * 获取随机数字和大写字母
     *
     * @param length 长度
     * @return 随机数字和字母
     */
    public static String getRandomNumberAndUpperLetters(int length) {
        Character[] characters = ArrayUtil.addAll(NUMBERS, UPPER_LETTERS);
        return getRandomString(characters, length);
    }

    /**
     * 获取随机字符串
     *
     * @param codeSeq 字符
     * @param length  长度
     * @return 随机字符串
     */
    private static String getRandomString(Character[] codeSeq, int length) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < length; i++) {
            String r = String.valueOf(codeSeq[ThreadLocalRandom.current().nextInt(codeSeq.length)]);
            s.append(r);
        }
        return s.toString();
    }


    /**
     * 替换Emoji表情
     *
     * @param source      源字符
     * @param replacement 要替换成的字符
     * @return 替换后的字符
     */
    public static String replaceEmoji(String source, String replacement) {
        if (isBlank(source)) {
            return source;
        }

        Pattern emoji = EMOJI_PATTERN;
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) {
            source = emojiMatcher.replaceAll(replacement);
            return source;
        }

        return source;
    }

    /**
     * 判断一个字符串是否是0数字。
     *
     * @param string
     * @return
     */
    public static boolean isNumber(String string) {
        if (string == null) {
            return false;
        }
        String regx = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regx);
        return pattern.matcher(string).matches();
    }
}
